package com.zhz.lib.project.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhz.lib.project.controller.form.NewsForm;
import com.zhz.lib.project.domain.news.Sortmap;
import com.zhz.lib.project.ibatis.domain.news.News;
import com.zhz.lib.project.service.news.NewsService;
import com.zhz.lib.project.vo.news.NewsVO;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.zhzutil.ZhzBeanUtils;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-3 下午03:22:27
 * 
 */
@Controller
@RequestMapping("/news.do")
public class NewsController {
    private static Logger logger      = Logger.getLogger(NewsController.class);
    private static int    firstResult = 0;
    private static int    maxResults  = 7;
    @Autowired
    private NewsService   newsService;
    @Autowired
    private IbatisPage    ibatisPage;

    private static String pageSql     = "selectAllNewsByPage";
    private static String countSql    = "selectAllNewsCount";

    @RequestMapping(method = RequestMethod.POST)
    public String getIbatisListPostNoAction(ModelMap model, NewsForm newsForm) {
        return page(model, newsForm);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getIbatisListGetNoAction(ModelMap model, NewsForm newsForm) {
        return page(model, newsForm);
    }

    @RequestMapping(method = RequestMethod.POST, params = "action=page")
    public String getIbatisListPost(ModelMap model, NewsForm newsForm) {
        return page(model, newsForm);
    }

    @RequestMapping(method = RequestMethod.GET, params = "action=page")
    public String getIbatisListGet(ModelMap model, NewsForm newsForm) {
        return page(model, newsForm);
    }

    @SuppressWarnings("unchecked")
    private String page(ModelMap model, NewsForm newsForm) {
        ZhzHttpRequestUtil.writeMapToModelMap(model, ibatisPage.getPage(ZhzBeanUtils
            .describe(newsForm), countSql, pageSql));

        // 将News转换为NewsVO
        List<News> newsList = (List<News>) model.get("list");
        List<NewsVO> newsVOList = new ArrayList<NewsVO>();
        if (newsList != null && newsList.size() > 0) {
            for (News news : newsList) {
                NewsVO newsVO = new NewsVO();
                BeanUtils.copyProperties(news, newsVO);
                newsVOList.add(newsVO);
            }
        }
        model.put("list", newsVOList);

        // 获取置顶最新消息
        List<NewsVO> topNewsVOList = newsService.getTopNews(firstResult, maxResults);
        model.put("topNewsVOList", topNewsVOList);

        // 获取最新消息类型
        List<Sortmap> typeList = newsService.getNewsType();
        model.put("typeList", typeList);

        return "news";
    }

    /**
     * 
     * 根据id获取最新消息
     * 
     * @param model
     * @param request
     * @param response
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET, params = "event=single")
    public String getSingleNews(ModelMap model, HttpServletRequest request,
                                HttpServletResponse response, @RequestParam("id") int id) {
        model.put("news", newsService.getNewsById(id));
        newsService.newsHitPlusOne(id);
        return "newsDetail";
    }

}
